package org.csu.jpetstoremanagement.service;

import org.csu.jpetstoremanagement.domain.Category;
import org.csu.jpetstoremanagement.domain.Item;
import org.csu.jpetstoremanagement.domain.Product;
import org.csu.jpetstoremanagement.persistence.CategoryMapper;
import org.csu.jpetstoremanagement.persistence.ItemMapper;
import org.csu.jpetstoremanagement.persistence.ProductMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CatalogService {

    @Autowired
    private CategoryMapper categoryMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private ItemMapper itemMapper;

    public Category getCategory(String categoryId) {
        return categoryMapper.getCategory(categoryId);
    }

    public List<Category> getCategoryList() {
        return categoryMapper.getCategoryList();
    }

    public void addCategory(Category category) {
        categoryMapper.insertCategory(category);
    }

    public void updateCategory(Category category) {
        categoryMapper.updateCategory(category);
    }

    public void deleteCategory(String categoryId) {
        categoryMapper.deleteCategory(categoryId);
    }

    public List<Product> getProductList() {
        return productMapper.getProductList();
    }

    public List<Product> getProductListByCategory(String categoryId) {
        return productMapper.getProductListByCategory(categoryId);
    }

    public Product getProduct(String productId) {
        return productMapper.getProduct(productId);
    }

    public void addProduct(Product product) {
        productMapper.insertProduct(product);
    }

    public void updateProduct(Product product) {
        productMapper.updateProduct(product);
    }

    public void deleteProduct(String productId) {
        productMapper.deleteProduct(productId);
    }

    public List<Product> searchProductList(String keyword) {
        return productMapper.searchProductList("%" + keyword.toLowerCase() + "%");
    }

    public List<Item> getItemList() {
        return itemMapper.getItemList();
    }

    public Item getItem(String itemId) {
        return itemMapper.getItem(itemId);
    }

    public List<Item> getItemListByProduct(String productId) {
        return itemMapper.getItemListByProduct(productId);
    }

    public boolean isItemInStock(String itemId) {
        return itemMapper.getInventoryQuantity(itemId) > 0;
    }

    public int getItemInventoryQuantity(String itemId) {
        return itemMapper.getInventoryQuantity(itemId);
    }

    public void addItem(Item item) {
        itemMapper.insertItem(item);
        itemMapper.insertInventory(item);
    }

    public void updateItem(Item item) {
        itemMapper.updateItem(item);
        itemMapper.updateInventory(item);
    }

    public void deleteItem(String itemId) {
        itemMapper.deleteItem(itemId);
        itemMapper.deleteInventory(itemId);
    }
}
